package interview;

public class lc34 {
    public static void main(String[] args) {
        int [] a = searchRange(new int[]{2,2},2);
        System.out.println(a[0] + " " + a[1]);
    }
    public static int[] searchRange(int[] nums, int target) {
        int[] re = new int[]{-1, -1};
        int n = nums.length;
        if(n == 0 || target > nums[n - 1] || target < nums[0])
            return re;
        /**
         * 寻找左右边界
         */
        int left = 0, right = n - 1;
        int a = -1, b = -1;
        while(left <= right){
            int middle = left + (right - left) / 2;
            if(nums[middle] >= target){
                a = middle;
                right = middle - 1;
            }
            else if(nums[middle] < target)
                left = middle + 1;
        }
        left = 0;
        right = n - 1;
        while(left <= right){
            int middle = left + (right - left) / 2;
            if(nums[middle] > target)
                right = middle - 1;
            else if(nums[middle] <= target){
                b = middle;
                left = middle + 1;
            }
        }
        if(a == -1 || b == -1 || a > b)
            return re;
        re[0] = a;
        re[1] = b;
        return re;
    }
}
